<template>
  <div class="container">
    <a-modal
      v-model:visible="visible"
      :width="1200"
      draggable
      :footer="false"
      @close="resetFields"
    >
      <template #title> {{ modalTitle }} </template>
      <a-form ref="formRef" layout="vertical" :model="formData">
        <a-row :gutter="16">
          <a-col :span="4">
            <a-form-item field="deviceName" label="设备名称">
              <a-input
                v-model="formData.deviceName"
                placeholder="请输入"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item field="model" label="设备型号">
              <a-input
                v-model="formData.model"
                placeholder="请输入"
                allow-clear
              />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="品牌" field="brand">
              <a-select
                v-model="formData.brand"
                placeholder="请选择"
                allow-clear
              >
                <a-option
                  :value="item.value"
                  v-for="item in brandOptions"
                  :key="item.value"
                  >{{ item.label }}</a-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item
              label="设备类型"
              field="deviceType"
              validate-trigger="input"
            >
              <a-select
                v-model="formData.deviceType"
                placeholder="请选择"
                allow-clear
              >
                <a-option
                  :value="item.value"
                  v-for="item in deviceTypeList"
                  :key="item.value"
                  >{{ item.label }}</a-option
                >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-button
              type="primary"
              :loading="loading"
              @click="fetchData"
              style="margin-top: 30px"
            >
              搜索
            </a-button>
          </a-col>
        </a-row>
      </a-form>
      <a-table
        row-key="id"
        :columns="columns"
        :data="state.list"
        :pagination="pagination"
        :loading="loading"
        @page-change="pageChange"
      >
        <template #index="{ rowIndex }">
          {{ rowIndex + 1 + (pagination.current - 1) * pagination.pageSize }}
        </template>
        <!-- <template #operations="{ record }">
          <a-dropdown trigger="click" :popup-max-height="false">
            <a-link>操作</a-link>
            <template #content>
              <a-doption>
                <a-space @click="handleEdit(record)">
                  <icon-edit />
                  <span> 编辑 </span>
                </a-space>
              </a-doption>
              <a-doption>
                <a-space @click="onDelete(record)">
                  <icon-delete />
                  <span> 删除 </span>
                </a-space>
              </a-doption>
            </template>
          </a-dropdown>
        </template> -->
      </a-table>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { costMeasurement, cfCarParkSearch } from '@/api/internetOfThings';
  import { cfDeviceGet } from '@/api/park';
  import { Message } from '@arco-design/web-vue';
  import { cloneDeep } from 'lodash';
  import dayjs from 'dayjs';

  const visible = ref(false);
  const loading = ref(false);
  const selectLoading = ref(false);
  const modalTitle = ref('硬件列表');
  const parkList = ref([]);
  const formRef = ref();
  const brandOptions = [
    {
      label: '臻识',
      value: 'zhen_shi',
    },
    {
      label: '华夏',
      value: 'hua_xia',
    },
    {
      label: '千熠',
      value: 'qian_yi',
    },
    {
      label: '海康',
      value: 'hk',
    },
    {
      label: '大华',
      value: 'dh',
    },
    {
      label: '道通物联',
      value: 'olm',
    },
    {
      label: '柏来',
      value: 'bo_lai',
    },
    {
      label: '驴充充',
      value: 'lv_chong_chong',
    },
    {
      label: '金盾',
      value: 'king_tun',
    },
    {
      label: '百芯',
      value: 'bai_xin',
    },
  ];
  const deviceTypeList = [
    { label: '常规车牌识别相机', value: 2 },
    { label: '匝道', value: 1 },
    { label: '高位相机', value: 3 },
    { label: '地锁', value: 4 },
    { label: 'led设备', value: 5 },
    { label: '监控相机', value: 6 },
    { label: '人脸识别机', value: 7 },
    { label: '充电桩', value: 8 },
  ];
  const pagination = reactive({
    current: 1,
    pageSize: 10,
    total: 0,
  });
  const formData = ref({
    deviceName: '',
    model: '',
    brand: '',
    deviceType: '',
  });
  const state = reactive({
    list: [],
  });

  const columns = [
    {
      title: 'id',
      dataIndex: 'id',
      slotName: 'id',
      width: 200,
    },
    {
      title: '设备名称',
      dataIndex: 'deviceName',
    },
    {
      title: '型号',
      dataIndex: 'model',
    },
    {
      title: '品牌',
      dataIndex: 'newBrand',
    },
    {
      title: '账号',
      dataIndex: 'username',
    },
    {
      title: '密码',
      dataIndex: 'password',
    },
    {
      title: '协议',
      dataIndex: 'networkProtocol',
    },
    // {
    //   title: '操作',
    //   dataIndex: 'operations',
    //   slotName: 'operations',
    // },
  ];
  const pageChange = (e: number) => {
    pagination.current = e;
    fetchData();
  };
  const checkParams = (obj: any) => {
    // eslint-disable-next-line no-restricted-syntax, guard-for-in
    for (const i in obj) {
      if (!obj[i]) {
        if (obj[i] !== 0) {
          obj[i] = null;
        }
      }
    }
    return obj;
  };
  const getLabelByValue = (value: any) => {
    const option = brandOptions.find((item: any) => item.value === value);
    return option ? option.label : '';
  };
  const fetchData = async () => {
    loading.value = true;
    const { code, total, data } = await cfDeviceGet({
      params: checkParams({
        page: pagination.current,
        size: pagination.pageSize,
        ...formData.value,
      }),
    });
    console.log(
      '🚀🚀🚀🚀🚀🚀🚀 ~ file: deviceList.vue:173 ~ fetchData ~ data:',
      data
    );
    loading.value = false;
    if (code === 10002) {
      if (total) pagination.total = total;
      for (const i of data) {
        i.newBrand = getLabelByValue(i.brand);
      }
      state.list = data;
    } else {
      state.list = [];
    }
  };
  const handleClick = () => {
    fetchData();
    visible.value = true;
  };

  const resetFields = () => {
    formRef.value.resetFields();
  };

  defineExpose({ handleClick });
</script>

<style scoped lang="less">
  .container {
    padding: 0 20px 40px 20px;
    overflow: hidden;
  }

  .actions {
    height: 60px;
    padding: 14px 20px 14px 0;
    background: var(--color-bg-2);
    text-align: right;
  }
</style>
